<?php
namespace app\admin\model;

use think\Model;

class Coupon extends Base
{
    protected $name = 'coupon';

    // 开启||关闭
    public function setIsShow($id){
        $info = $this->getFind(['id' => $id])['data'];

        if($info['is_show'] == 0){
            $is_show      = 1;
            $back['flag'] = 1;
            $back['msg']  = '开启成功';
        }else{
            $is_show      = 0;
            $back['flag'] = 0;
            $back['msg']  = '关闭成功';
        }

        $data = [
            'id'      => $id,
            'is_show' => $is_show,
        ];
        
        $this->update($data);
        return $back;
    }

    public function getAdminIdAttr($val){
        return (new Admin())->getFind(['id' => $val])['data'];
    }

    public function getScopeAttr($val){
        $type = [1 => '实体店', 2 => '线上', 3 => '会员专享'];
        $arr = explode(',', $val);
        array_pop($arr);

        $count = count($arr);

        $backData = '';
        foreach($arr as $k => $v){
            $backData .= $type[$v];

            if($count > $k + 1){
                $backData .= ',';
            }
        }
        
        return $backData;
    }

    public function getStartTimeAttr($val){
        if(!$val){
            return '';
        }

        return date('Y-m-d', $val);
    }

    public function getEndTimeAttr($val){
        if(!$val){
            return '';
        }

        return date('Y-m-d', $val);
    }
}